package sdk.webview.fmc.com.fmcsdk.presenter;

import android.text.TextUtils;
import android.widget.Toast;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseData;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.base.CommonApi;
import sdk.webview.fmc.com.fmcsdk.interfaces.FeedBackView;
import sdk.webview.fmc.com.fmcsdk.util.ToastUtils;
import sdk.webview.fmc.com.fmcsdk.util.Url;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ResponseTransformer;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.ResponseSubscriber;


/**
 * Created by ydm on 2017/9/14.
 */

public class FeedBackPresenter extends BasePresenter<FeedBackView> {
    /**
     * 提交意见
     * @param text 意见内容
     */
    public void submit(final String text) {
        if (TextUtils.isEmpty(text)){
            ToastUtils.showShort(view.getViewContext(),view.getViewContext().getString(R.string.input_feed_back));
            return;
        }
        view.showLoading();
        getApi(CommonApi.class).submitFeedBack(getDefaultAddress()+ Url.FEEDBACK, UserHelper.getToken(),text)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>(view) {
                    @Override
                    public void success(BaseData baseData) {
                        ToastUtils.showShort(view.getViewContext(),view.getViewContext().getString(R.string.submit_ok));
                        view.submitSuccess();
                    }
                    @Override
                    public void onRequestAgain() {
                        submit(text);
                    }
                });
    }
}
